using System;
using System.Collections.Generic;
using System.Text;

namespace 修为尽失.辅助.exts
{
     public static class Number
    {
        public static Random RandomRandom()
        {
            return new Random(Guid.NewGuid().GetHashCode());
        }

        public static Tuple<int, int> RandomDoubleInt(int min1,int max1,int min2,int max2)
        {
            return new Tuple<int, int>(RandomRandom().Next(min1,max1),RandomRandom().Next(min2,max2));
        }
        public static double NextDouble_负一到一(this Random ran)
        {
            return ran.Next(-1, 2) * ran.NextDouble();
        }

        public static byte[] NextBytes(this Random ran, int length)
        {
            var bs = new byte[length];
            ran.NextBytes(bs);
            return bs;
        }

        public static byte ToByte(this int value)
        {
            try
            {
                return BitConverter.GetBytes(value)[0];
            }
            catch
            {
                return 0;
            }
        }

        public static string ToBinary(this byte value) => Convert.ToString(value, 2);
        public static string To16band(this byte value) => value.ToString("X2");

        public static string To16band(this byte[] values, string separator = "")
        {
            if ((values == null) || (values.Length <= 0))
                return string.Empty;
            var stringBuilder = new StringBuilder();
            foreach (var num in values)
                stringBuilder.Append(num.To16band()).Append(separator);
            return stringBuilder.ToString();
        }

        public static bool IsGreaterThanZero(this int value) => value > 0;

        public static bool IsGreaterThanZero(this int? value)
        {
            if (!value.HasValue)
                return false;
            var nullable = value;
            return (nullable.GetValueOrDefault() > 0) && nullable.HasValue;
        }

        public static bool Is0OrNaN(this double d) => double.IsNaN(d) || d == 0;

        public static char HexToChar(this int a)
        {
            a &= 15;
            return a > 9 ? (char) (a - 10 + 97) : (char) (a + 48);
        }

        public static long Reverse(this long i) => int.Parse((i > 0 ? string.Empty : "-") + new string(new Stack<char>((IEnumerable<char>) Math.Abs(i).ToString()).ToArray()));
        public static int Reverse(this int i) => int.Parse((i   > 0 ? string.Empty : "-") + new string(new Stack<char>((IEnumerable<char>) Math.Abs(i).ToString()).ToArray()));
        public static int LengthToo(this ulong i) => i.ToString().Length;

        public static int Length(this ulong i)
        {
            if (i < 10000000000)
            {
                if (i < 10)
                    return 1;
                if (i < 100)
                    return 2;
                if (i < 1000)
                    return 3;
                if (i < 10000)
                    return 4;
                if (i < 100000)
                    return 5;
                if (i < 1000000)
                    return 6;
                if (i < 10000000)
                    return 7;
                if (i < 100000000)
                    return 8;
                if (i < 1000000000)
                    return 9;
                return 10;
            }
            else
            {
                if (i < 100000000000)
                    return 11;
                if (i < 1000000000000)
                    return 12;
                if (i < 10000000000000)
                    return 13;
                if (i < 100000000000000)
                    return 14;
                if (i < 1000000000000000)
                    return 15;
                if (i < 10000000000000000)
                    return 16;
                if (i < 100000000000000000)
                    return 17;
                if (i < 1000000000000000000)
                    return 18;
                if (i < 10000000000000000000)
                    return 19;
                return 20;
            }
        }
    }
}